<?php
namespace app\controllers;


use app\common\corelib\CacheHelper;
use app\common\corelib\LocalUpload;
use app\logic\ShopLogic;
use app\logic\UserLogic;
use app\models\Goods;
use app\models\MallGoods;
use yii;

class MallController extends BackendController
{
    const LIMIT=30;

    public function actionIndex(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $shopLogic = new ShopLogic();
        $this->data['breadcrumbs'] = '商城列表';
        return $this->render('index', [
            'mall' => $shopLogic->getMall($query, $page),
            'query' => $query
        ]);
    }

    public function actionAdd(){
        $shopLogic = new ShopLogic();
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['商城列表' => U('mall/index'), ' 添加商城' => ''];
            $userLogic = new UserLogic();
            $partner = $userLogic->getPartners();
            return $this->render('add', [
                'partner'=>$partner,
                'theme' => '/assets/',
                'actionUrl' => U('mall/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            if($_FILES['mall_logo']['name']){
                LocalUpload::go_upload($_FILES['mall_logo']);
                $uploadresult1 = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult1 = '';
            }
            if($_FILES['adv_pic']['name']){
                LocalUpload::go_upload($_FILES['adv_pic']);
                $uploadresult2 = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult2 = '';
            }
            $logicRes = $shopLogic->addMall($params,$uploadresult1,$uploadresult2);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增商城成功');
                jump('mall/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增商城失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionEdit(){
        $shopLogic = new ShopLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('mall_id');
            $page = Yii::$app->request->get('page', 1);
            $params = Yii::$app->request->get('form');
            if(!$id)
                $id = $params['mall_id'];
            $mall_goods = $shopLogic->getMallGoodsById($id,$params,$page);
            $mall = $shopLogic->getMallById($id);
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$mall['mall_logo'])){
                $mall_logo = $mall['mall_logo'];
            }else{
                $mall_logo = '/default.jpg';
            }
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$mall['adv_pic'])){
                $adv_pic = $mall['adv_pic'];
            }else{
                $adv_pic = '/default.jpg';
            }
            $userLogic = new UserLogic();
            $partner = $userLogic->getPartners();
            $this->data['breadcrumbs'] = ['商城管理' => U('mall/index'), $mall['mall_name'] => '', '更新' => ''];
            return $this->render('edit', [
                'partner'=>$partner,
                'mall_logo'=>$mall_logo,
                'adv_pic'=>$adv_pic,
                'mall'=>$mall,
                'mall_goods'=>$mall_goods,
                'theme' => '/assets/',
                'actionUrl' => U('mall/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('mall_id');
            if($_FILES['mall_logo']['name']){
                LocalUpload::go_upload($_FILES['mall_logo']);
                $uploadresult1 = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult1 = '';
            }
            if($_FILES['adv_pic']['name']){
                LocalUpload::go_upload($_FILES['adv_pic']);
                $uploadresult2 = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult2 = '';
            }
            $logicRes = $shopLogic->editMallById($id, $params,$uploadresult1,$uploadresult2);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新商城成功');
                jump('mall/index');
            } else {
                Yii::$app->getSession()->setFlash('error','更新商城失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionSaveStatus()
    {
        $id = Yii::$app->request->get('mall_id');
        $shopLogic = new ShopLogic();
        $logicRes = $shopLogic->saveMallStatus($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','更改商城状态成功');
        } else {
            Yii::$app->getSession()->setFlash('error','更改商城状态失败');
        }
        return $this->redirect(U('mall/index'));
    }

    public function actionGetGoods(){
        $mall_id = Yii::$app->request->post('mall_id');
        $ids = MallGoods::find()->select('goods_id')->where(['mall_id'=>$mall_id])->asArray()->all();
        if($ids){
            $ids = array_column($ids,'goods_id');
            $result = Goods::find()->where(['not in','goods_id',$ids])->asArray()->all();
        }else{
            $result = Goods::find()->asArray()->all();
        }
        echo json_encode($result);
    }

    public function actionAddGoods(){
        $mall_id = Yii::$app->request->post('mall_id');
        $goods_id = Yii::$app->request->post('goods_id');
        $shopLogic = new ShopLogic();
        $result = $shopLogic->addGoods($mall_id,$goods_id);
        echo json_encode($result);
    }

    public function actionDeleteGoods(){
        $mall_id = Yii::$app->request->get('mall_id');
        $goods_id = Yii::$app->request->get('goods_id');
        $shopLogic = new ShopLogic();
        $logicRes = $shopLogic->deleteGoods($mall_id,$goods_id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        return $this->redirect(U(['mall/edit','mall_id'=>$mall_id]));
    }

    public function actionSortGoods(){
        $mall_id = Yii::$app->request->post('mall_id');
        $goods_id = Yii::$app->request->post('goods_id');
        $sort = Yii::$app->request->post('sort');
        $shopLogic = new ShopLogic();
        $result = $shopLogic->sortGoods($mall_id,$goods_id,$sort);
        echo json_encode($result);
    }

}
